package com.aaa.service;

import com.aaa.dao.UserInfoDao;
import com.aaa.pojo.ChangeMoney;
import com.aaa.pojo.Credit;
import com.aaa.pojo.UserInfo;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public UserInfo loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo user = userInfoDao.queryByName(username);
        if (user == null) {
//            throw new UsernameNotFoundException("不存在该用户!");
            return null;
        }
        return user;
    }

    public UserInfoDao getUserInfoDao() {
        return userInfoDao;
    }

    public UserInfo queryById(Integer uid){
        UserInfo userInfo = userInfoDao.queryById(uid);
        if(userInfo == null){
            return null;
        }
        return userInfo;
    }
    public Map<String,Object> queryUserDetails(Integer uid){
        return userInfoDao.queryUserDetails(uid);
    }
    public Boolean reg_insert(String username,String password){
        return userInfoDao.reg_insert(username,password);
    }
    public Boolean updateUserDetails(JSONObject jsonObject){
        return userInfoDao.updateUserDetails(jsonObject);
    }
    public Boolean insertUserDetails(JSONObject jsonObject){
        return userInfoDao.insertUserDetails(jsonObject);
    }
    public Boolean updateVisits(String auths,Integer uid){return userInfoDao.updateVisits(auths,uid);}
    //根据用户id查询密码
    public String selPwdByUid(Integer uid){
        return userInfoDao.selPwdByUid(uid);
    }
    //根据用户名修改密码
    public Boolean updatePwdByUid(Integer uid,String password){
        return userInfoDao.updatePwdByUid(uid,password);
    }
    //信誉值问题
    public PageInfo<Credit> getUserCreditByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<Credit> list = userInfoDao.selectAllCredict();
        PageInfo<Credit> pageInfo = new PageInfo<Credit>(list);
        return pageInfo;
    }
    public Credit selectCreditById(Integer uid){
        return userInfoDao.selectCreditById(uid);
    }
    public List<Credit> AllCredit(){
        return userInfoDao.AllCredit();
    }
    public   Boolean updateUserCredit(Integer uid,Integer cd_id){
        return userInfoDao.updateUserCredit(uid, cd_id);
    }

    public List<UserInfo> queryUserAll(){
        return userInfoDao.queryUserAll();
    }
}
